package com.depth.viewer.request.webservice
{
	import com.depth.viewer.utils.Hashtable;
	
	import flash.net.Responder;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.LoadEvent;
	import mx.rpc.soap.WebService;

	public class WebServiceServcie
	{
		
		private var _resultHandle:Function;
		private var _faultHandle:Function;
		private var _completeHandle:Function;
		
		private var _actions:ArrayCollection = new ArrayCollection();
		
		public var service:WebService 
		public function WebServiceServcie(wsdl:String = null)
		{
			service= new WebService();
			service.wsdl = wsdl;
			service.loadWSDL();
			service.addEventListener(ResultEvent.RESULT,resultEventHandle);
			service.addEventListener(FaultEvent.FAULT,faultEventHane);
			service.addEventListener(LoadEvent.LOAD,wsdlLoadedHandle);
		}
		public function call(methodName:String,args:Array,token:Object = null):void{
			if(service.ready){
				var _token:AsyncToken;
				switch(args.length){
					case 0:
						_token = service.getOperation(methodName).send();
						break;
					case 1:
						_token = service.getOperation(methodName).send(args[0]);
						break;
					case 2:
						_token = service.getOperation(methodName).send(args[0],args[1]);
						break;
					case 3:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2]);
						break;
					case 4:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3]);
						break;
					case 5:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4]);
						break;
					case 6:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5]);
						break;
					case 7:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
						break;
					case 8:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
						break;
					case 9:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
						break;
					case 10:
						_token = service.getOperation(methodName).send(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
						break;
					
				}
			}else{
				_actions.addItem({methodName:methodName,params:args,token:token});
			}
		}
		private function resultEventHandle(event:ResultEvent):void{
			if(_resultHandle){
				_resultHandle(event.result);
			}
			if(_completeHandle){
				_completeHandle();
			}
		}
		private function faultEventHane(event:FaultEvent):void{
			if(_faultHandle){
				_faultHandle(event.fault.message);
			}
		}
		private function wsdlLoadedHandle(event:LoadEvent):void{
			event.target.removeEventListener(LoadEvent.LOAD,wsdlLoadedHandle);
			var actions:Array = _actions.toArray();
			while(actions.length > 0){
				var action:Object = actions.shift();
				call(action.methodName,action.params);
			}
		}
		
		public function set resultHandle(value:Function):void{
			this._resultHandle = value;
		}
		public function set faultHandle(value:Function):void{
			this._faultHandle = value;
		}
		public function set completeHandle(value:Function):void{
			this._completeHandle = value;
		}
	}
}